package com.test.vcharts.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.test.vcharts.controller.UrlController;
import com.test.vcharts.model.KModel;
import com.test.vcharts.service.IStockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service
public class StockServiceImpl implements IStockService {
    @Value("${stock.api}")
    private String stockApi;
    @Autowired
    private RestTemplate restTemplate;
    private final static Logger LOGGER = LoggerFactory.getLogger(StockServiceImpl.class);
    @Override
    public Object queryResult(String stockCode, Integer period) {
        if(stockCode==null ||"".equals(stockCode)){
            return Collections.emptyList();
        }
        period=(period==null||"".equals(period))?5*60:period;
        try {
            //请求体
            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();

            //设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.add("Accept", MediaType.TEXT_PLAIN_VALUE);
            headers.add("Cache-Control", "no-cache");
            HttpMethod requestMethod = HttpMethod.GET;
            String api = String.format(stockApi, stockCode, period);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, headers);
            ResponseEntity<String> exchange = restTemplate.exchange(api,
                    requestMethod, httpEntity, String.class);
           Object result=exchange.getBody();

         Gson gson = new Gson();
            LOGGER.debug("request-result:{}", gson.toJson(exchange.getBody()));
            //解析数据
            if("null".equals(result)){
                return Collections.emptyList();
            }
            List<KModel> kModelList=JSON.parseArray((String) result,KModel.class);
           return kModelList.stream().map(i->{
                Map map=new ConcurrentHashMap();
                KModel kModel=(KModel) i;
                map.put("日期",kModel.day);
                map.put("open",kModel.open);
                map.put("close",kModel.close);
                map.put("highest",kModel.high);
                map.put("lowest",kModel.low);
                map.put("vol",kModel.volume);
                return map;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }
}
